[<<Previous Entry]
[^^Up^^]
[Next Entry>>]
[Menu]
[About The Guide]
lock() Set File Sharing Locks
#include <io.h>
int lock(handle,offset,length);
int handle; Handle associated with file
long offset; Location of area to be locked
long length; Length of area to be locked
lock() locks an area of the file associated with 'handle', starting
at the position 'offset' and proceeding for 'length' bytes. Locking
bytes in a file prevents subsequent reading and writing of those
bytes by other processes. No overlapping regions can be locked. A
program trying to read or write into a locked region will retry the
operation three times. If all three retries fail, the call fails
with an error.
lock() must be removed before a file is closed, and the program must
release all locks before completing.
Returns: 0, if successful; -1 on error
Notes: lock() works only with MS-DOS versions 3.0 and later.
There is no support for file locks in older versions.
Portability: MS-DOS 3.0 and later, only.
-------------------------------- Example ---------------------------------
The following statements open a file, lock the first 1000 bytes of
the file, do some operation on it and then unlock the file.
#include <stdio.h> /* for printf */
#include <io.h> /* for lock, unlock, _open and close */
#include <fcntl.h> /* for constants */
main()
{
int handle;
if ((handle =
_open("\network\shared.dat",O_RDWR|O_DENYNONE)) == -1) {
printf("Error opening file.\n");
exit(1);
}
if (lock(handle,0L,1000L) == -1) {
printf("Unable to lock file.\n");
exit(1);
}
.
.
/* do some file operations of first 1000 bytes */
.
.
unlock(handle,0L,1000L);
closes(handle);
}
See Also:
open()
unlock()
This page created by ng2html v1.05, the Norton guide to HTML conversion utility.
Written by Dave Pearson